/**
 * $version:  0.1 
 * $Date: 2012-04-15 
 *
 * Copyright (C) 2010-2011 Jawa Software. All rights reserved.
 *
 */

package org.jawa.core.cluster;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.HashMap;
import java.util.Map;

import org.jawa.core.cache.CacheFactory;
import org.jawa.core.cache.ClusterTask;

/**
 * 返回当前集群成员的基本信息,包括内存的使用状况等.
 *
 */
public class GetBasicStatistics implements ClusterTask {
	public static final String NODE = "node";
	public static final String MEMORY_CURRENT = "memory_cur";
	public static final String MEMORY_MAX = "memory_max";

	private Map<String, Object> values;

	public Object getResult() {
		return values;
	}

	public void run() {
		values = new HashMap<String, Object>();
		values.put(NODE, CacheFactory.getClusterMemberName());
		Runtime runtime = Runtime.getRuntime();
		double freeMemory = (double) runtime.freeMemory() / (1024 * 1024);
		double maxMemory = (double) runtime.maxMemory() / (1024 * 1024);
		double totalMemory = (double) runtime.totalMemory() / (1024 * 1024);
		double usedMemory = totalMemory - freeMemory;
		values.put(MEMORY_CURRENT, usedMemory);
		values.put(MEMORY_MAX, maxMemory);
	}

	public void writeExternal(ObjectOutput out) throws IOException {
		// 忽略
	}

	public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
		// 忽略
	}
}
